﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;

using System.Security;
using System.Security.Cryptography;
using System.IO;

using System.Reflection; // 引用这个才能使用Missing字段
using System.Xml;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Business
{
    public partial class LimitAJ : Form
    {
        public static string sDataBaseIp;
        public static string sDataBaseName;
        public static string sDataBaseUserName;
        public static string sDataBasePwd;
        private TextBox[] ctextbox;
        public static string[] czdm;

        public LimitAJ()
        {
            InitializeComponent();
           
            ctextbox = new TextBox[60] { textBox1, textBox2, textBox3, textBox4, textBox5, textBox6, textBox7, textBox8, textBox9, textBox10, textBox11, textBox12, textBox13, textBox14, textBox15, textBox16, textBox17, textBox18, textBox19, textBox20, textBox21, textBox22, textBox23, textBox24, textBox25, textBox26, textBox27, textBox28, textBox29, textBox30, textBox31, textBox32, textBox33, textBox34, textBox35, textBox36, textBox37, textBox38, textBox39, textBox40, textBox41, textBox42, textBox43, textBox44, textBox45, textBox46, textBox47, textBox48, textBox49, textBox50, textBox51, textBox52, textBox53, textBox54, textBox55, textBox56, textBox57, textBox58, textBox59, textBox60 };
            czdm = new string[60] { "XZ_1", "XZ_2", "XZ_3", "XZ_4", "XZ_5", "XZ_6", "XZ_7", "XZ_8", "XZ_9", "XZ_10", "XZ_11", "XZ_12", "XZ_13", "XZ_14", "XZ_15", "XZ_16", "XZ_17", "XZ_18", "XZ_19", "XZ_20", "XZ_21", "XZ_22", "XZ_23", "XZ_24", "XZ_25", "XZ_26", "XZ_27", "XZ_28", "XZ_29", "XZ_30", "XZ_31", "XZ_32", "XZ_33", "XZ_34", "XZ_35", "XZ_36", "XZ_37", "XZ_38", "XZ_39", "XZ_40", "XZ_41", "XZ_42", "XZ_43", "XZ_44", "XZ_45", "XZ_46", "XZ_47", "XZ_48", "XZ_49", "XZ_50", "XZ_51", "XZ_52", "XZ_53", "XZ_54", "XZ_55", "XZ_56", "XZ_57", "XZ_58", "XZ_59", "XZ_60" };
          //  getbiaozhun();


        }
      
        public void getbiaozhun()
        {
            for (int i = 0; i < 60; i++)
            {
                ctextbox[i].Text =  Decrypt(GetDBString(string.Format(@"select {0} from Standsjm where XZ_ID=1 ", czdm[i]), czdm[i]));
            }
           

        }
        private string Encryption(string express)//jia
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        public bool InsertDB(string sql)
        {

            string connectstr = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
            SqlConnection sqlcnn = new SqlConnection(connectstr);
            //try
            {
                //打开数据库
                sqlcnn.Open();
                SqlCommand cmd = new SqlCommand(sql, sqlcnn);
                //执行插入操作，并返回受影响的行数
                int i = cmd.ExecuteNonQuery();
                //如果受影响的行数小于一则说明操作失败了
                if (i < 1)
                    return false;
                //关闭数据库
                sqlcnn.Close();
            }
            //catch (Exception er)
            //{
            //    string temp = er.Message.ToString();
            //    MessageBox.Show(temp);
            //    return false;
            //}
            return true;
        }
        public int selectDBcount(string sql)//查询该SQL语句的返回结果
        {

       
            int icount = 0;

            //try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection cnn = new SqlConnection(connectionstring);
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                DataTable dt = ds.Tables[0];//结果在这个table里，把它作为数据源就好了
                cnn.Close();
                icount = ds.Tables[0].Rows.Count;
                return icount;
            }
            //catch (Exception ex2)
            //{
            //    MessageBox.Show(ex2.ToString());
            //    return 0;
            //}

        }
        public string GetDBString(string sql, string DBname)
        {

           
            string m_stest;
            m_stest = "";
            //try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIp, sDataBaseName, sDataBaseUserName, sDataBasePwd);
                SqlConnection cnn = new SqlConnection(connectionstring);
                cnn.Open();
                SqlCommand cmd = new SqlCommand(sql, cnn);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                System.Data.DataTable dt = ds.Tables[0];//结果在这个table里，把它作为数据源就好了
                foreach (DataRow myDataRow in dt.Rows)
                {

                    m_stest = myDataRow[DBname].ToString().Trim();
                }
                cnn.Close();

            }
            //catch (Exception ex2)
            //{
            //    MessageBox.Show(ex2.ToString());
            //    return m_stest;

            //}
            return m_stest;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < 60; i++)
            {
                InsertDB(string.Format(@"update Standsjm set {0}='{1}' where XZ_ID=1 ", czdm[i], Encryption(ctextbox[i].Text)));
            }
        }

        private void label57_Click(object sender, EventArgs e)
        {

        }

        private void textBox71_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox68_TextChanged(object sender, EventArgs e)
        {

        }
        public MainForm m_mainform;
        private void LimitAJ_Load(object sender, EventArgs e)
        {
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("检测合格标准查看"))
            {
                m_mainform.baocunhxrj("检测合格标准查看", string.Format("检测合格标准查看"),"查询");// 核心功能日记  日记内容
            }

            //判断是否民警
            if (m_mainform.sfjy.Contains("检测合格标准查看"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    MessageBox.Show("警员功能，非警员不允许查看");
                    this.Close();
                }
            }

        }




    }
}
